package com.yunxi.service.dubboService.impl.dealer;

import com.aiways.center.channel.api.IChannelApi;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dtyunxi.rest.RestResponse;
import com.github.pagehelper.PageInfo;
import com.yunxi.baseDataToHub.pojo.iam.PmsOrganizeVO;
import com.yunxi.baseDataToHub.service.iam.IamDubboService;
import com.yunxi.core.util.WgrDate;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.dealer.pojo.PartnerTwonetsVO;
import com.yunxi.dealer.service.PartnerTwonetsDubboService;
import com.yunxi.model.dealer.PartnerTwonets;
import com.yunxi.service.localService.dealer.PartnerTwonetsService;
import com.yunxi.util.S2SEnums;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 经销商二网信息 服务实现类
 * </p>
 *
 * @author
 * @since 2019-05-20
 */
//@com.alibaba.dubbo.config.annotation.Service(version = "${demo.service.version}",
//        application = "${dubbo.application.id}",
//        protocol = "${dubbo.protocol.id}",
//        registry = "${dubbo.registry.id}"
//)
public class PartnerTwonetsDubboServiceImpl implements PartnerTwonetsDubboService {

   private static final Logger logger = LoggerFactory.getLogger(PartnerTwonetsDubboServiceImpl.class);

   @Autowired
   @Qualifier(value = "partnerTwonetsServiceImpl")
   PartnerTwonetsService partnerTwonetsService;

   @Autowired
   IamDubboService iamDubboServiceImpl;

   @Autowired
   IChannelApi channelApi;

   @Override
   public PartnerTwonetsVO getById(String id) throws InvocationTargetException, IllegalAccessException {
      PartnerTwonets partnerTwonets = partnerTwonetsService.getById(id);
      PartnerTwonetsVO partnerTwonetsVO = new PartnerTwonetsVO();
      BeanUtils.copyProperties(partnerTwonets, partnerTwonetsVO);
      return partnerTwonetsVO;
   }

   @Override
   public void saveOrUpdate(PartnerTwonetsVO partnerTwonetsVO) throws InvocationTargetException, IllegalAccessException {
      PartnerTwonets partnerTwonets = new PartnerTwonets();
      BeanUtils.copyProperties(partnerTwonetsVO, partnerTwonets);
      partnerTwonetsService.saveOrUpdate(partnerTwonets);
   }

   @Override
   @Transactional
   public void removeById(String id, String partnerCode, LoginInfo loginInfo) throws Exception {
      PartnerTwonets partnerTwonets = partnerTwonetsService.getById(id);
      partnerTwonets.setUpdateBy(loginInfo.getRealName());
      //将渠道二网信息传入中台
      RestResponse<Void> voidRestResponse = null;
      try {
         voidRestResponse = channelApi.delChannelTwonets(partnerTwonets.getTwoCode());
      } catch (Exception e) {
         TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
         logger.error(" ------- 中台调用出错 ___< 删除渠道二网信息  >--------"+e.getMessage());
         e.printStackTrace();
         return;
      }
      if (!voidRestResponse.getResultCode().equals("0")) {
         TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
         logger.error(" ------- 中台执行失败 ___< 删除渠道二网信息  >--------"+voidRestResponse.getResultMsg());
         return;
      }
      //将渠道二网组织机构信息传入IAM
      if(pms2IAM(partnerTwonets,S2SEnums.OperatorType.delete.getKey())) return;
      partnerTwonetsService.removeById(id);
   }

   @Override
   public PageInfo<PartnerTwonetsVO> findPageByVO(PartnerTwonetsVO partnerTwonetsVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {

      PageInfo<PartnerTwonetsVO> pageInfo = partnerTwonetsService.pageByVo(partnerTwonetsVO, pageNum, pageSize);

      return pageInfo;

   }

   //渠道二网 组织机构IAM对接
   public boolean pms2IAM(PartnerTwonets partnerTwonets, String op) throws Exception {
      //判断是否 新增 或 更新 还是删除
      List list = new ArrayList();

      PmsOrganizeVO vo = new PmsOrganizeVO();

      vo.setPartnerCode(partnerTwonets.getPartnerCode());
      vo.setSuperiorPartnerCode(partnerTwonets.getPartnerCode());
      vo.setPartnerStatus(partnerTwonets.getStatus() + "");
      vo.setOperator(op);
      vo.setUpdateTime(WgrDate.getCurrentTime(3));
      list.add(vo);
      ApiResult apiResult = iamDubboServiceImpl.sendIamOrganizeVO(list);
      if (apiResult.getCode().equals(-1)) {
         TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
         logger.error("  ------- iamDubboServiceImpl.sendIamOrganizeVO 执行失败 ___渠道二网 组织机构  ---------  ");
         return true;
      }
      return false;
   }
}
